﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Presenters;
using University.Workshop.Model;
namespace University.Workshop.WinApp
{
    public partial class FormProviderView : Form,IProvider
    {
        private PresenterProvider  presenter;
        Variables flag = Variables.Instance;
        PurchaseVariables variablePurchase = PurchaseVariables.Instance;
        public FormProviderView()
        {
            InitializeComponent();
            presenter = new PresenterProvider(this);

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (ProviderControl.RegisterProvider(textBoxAddress.Text, textBoxProviderContact.Text, textBoxMail.Text, textBoxName.Text, textBoxWeb.Text, textBoxCellular.Text, textBoxNIT.Text, textBoxTelephone.Text))
                {
                    MessageBox.Show(this, "El registro se llevo a cabo satisfactoriamente ", "Registro Satisfactorio", MessageBoxButtons.OK,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                    foreach (Control c in tabPageRegisterProvider.Controls) { 
                        if(c is TextBox){
                            c.Text = string.Empty;
                        }
                    }
                    tabProviderGestor.SelectedTab=tabPageMainProvider;
                }
            }
           
            catch (BusinessException be)
            {
                MessageBox.Show(this, be.Message,"Parameters incorrects", MessageBoxButtons.OK,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
            }
        }

        private void btnRegisterProvider_Click(object sender, EventArgs e)
        {
            tabProviderGestor.SelectedTab = tabPageRegisterProvider;
        }

        private void btnQuitRegister_Click(object sender, EventArgs e)
        {
            foreach (Control c in tabPageRegisterProvider.Controls)
            { 
                    
           if(c is TextBox){
                        c.Text = string.Empty;
                    }
                    }
                    tabProviderGestor.SelectedTab=tabPageMainProvider;
    }

        private void btnClean_Click(object sender, EventArgs e)
        {
            foreach (Control c in tabPageRegisterProvider.Controls) {
                if (c is TextBox) {
                    c.Text = string.Empty;

                }
            }

        }

        private void JustNumber(object sender, KeyPressEventArgs e)
        {
            Validator.ValidatorOnlyNumbers(sender, e);
        }

        private void NumbersLetters(object sender, KeyPressEventArgs e)
        {
            if (char.IsControl(e.KeyChar) || char.IsNumber(e.KeyChar) || char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            {
                e.Handled = false;
            }
            else {
                e.Handled = true;
            }
        }

        private void CharWeb(object sender, KeyPressEventArgs e)
        {
            if (char.IsControl(e.KeyChar) || char.IsNumber(e.KeyChar) || char.IsPunctuation(e.KeyChar)||(e.KeyChar=='@')||char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void btnQuitProvider_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuitSearch_Click(object sender, EventArgs e)
        {
            cleanSearch();
            tabProviderGestor.SelectedTab = tabPageMainProvider;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {

                DataTable misresp= presenter.Search();
                if (misresp.Rows.Count!=0)
                {
                    dataGridViewProvider.DataSource = misresp;
                    panelSearchProvider.Visible = true;
                    dataGridViewProvider.SelectedCells[0].Selected = false;
                    enablePanel.Visible = checkBoxActive.Checked;

                }
                

            }
            catch (BusinessException be) {
                MessageBox.Show(this, be.Message, "Parametro incorrecto", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
            }
           
        }

        #region MembersSearch

        public string Search
        {
            get { return txtSearch.Text; }
        }
        public bool Active
        {
            get { return checkBoxActive.Checked; }
        }
        #endregion
        #region MembersProviderModify
        public string ModifyName { get { return modifNombre.Text; } set { modifNombre.Text = value; } }
        public string ModifyEmail { get { return modifEmail.Text; } set { modifEmail.Text = value; } }
        public string ModifyWeb { get { return modifWeb.Text; } set { modifWeb.Text = value; } }
        public string ModifyContact { get { return modifContact.Text; } set { modifContact.Text = value; } }
        public string ModifyCellular { get { return modifCellular.Text; } set { modifCellular.Text = value; } }
        public string ModifyTelephone { get { return modifPhone.Text; } set { modifPhone.Text = value; } }
        public string ModifyAddress { get { return modifAddress.Text; } set { modifAddress.Text = value; } }
        public string ModifyNit { get { return dataGridViewProvider.CurrentRow.Cells[0].Value.ToString(); } set { modifNit.Text = value; } }
        #endregion

        private void btnSearchProvider_Click(object sender, EventArgs e)
        {
            tabProviderGestor.SelectedTab = tabPageSearchProvider;
        }

        private void btnModifyProvider_Click(object sender, EventArgs e)
        {
            try
            {
                IsRowSelected();
                cleanSearch();
                presenter.SetProvider();
                label11.Visible = true;
                label12.Visible = false;
                foreach (Control c in tabPageEditProvider.Controls)
                {
                    if (c is TextBox)
                    {
                        c.Enabled = true;
                    }
                }
                modifNit.Enabled = false;
                ModifySave.Visible = true;
                modifClean.Visible = true;
                tabProviderGestor.SelectedTab = tabPageEditProvider;
            }
            catch (BusinessException be) {
                MessageBox.Show(this, be.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
            
            }
        }
        private void enableButton_Click(object sender, EventArgs e)
        {
            try
            {
                IsRowSelected();
                cleanSearch();
                if (presenter.EnableProvider()) {
                    MessageBox.Show(this, "El Proveedor Fue Habilitado Exitosamente", "Felicitaciones!!!!", MessageBoxButtons.OK,
            MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
            MessageBoxOptions.RightAlign);
                }
            }
            catch (BusinessException be)
            {
                MessageBox.Show(this, be.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
            }
        }

        private void btnDeleteProvider_Click(object sender, EventArgs e)
        {
            try
            {
                IsRowSelected();
                cleanSearch();
                if(presenter.DeleteProvider()){
                MessageBox.Show(this, "El Proveedor Fue Inhabilitado Exitosamente", "Felicitaciones!!!!", MessageBoxButtons.OK,
            MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
            MessageBoxOptions.RightAlign);
                }
            }
            catch (BusinessException be)
            {
                MessageBox.Show(this, be.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
            }

        }

        private void btnShowProvider_Click(object sender, EventArgs e)
        {
            try
            {
            IsRowSelected();
            presenter.SetProvider();
            cleanSearch(); 
            label11.Visible = false;
            label12.Visible = true;
            foreach (Control c in tabPageEditProvider.Controls)
            {
                if (c is TextBox)
                {
                    c.Enabled = false;
                }
            }
            ModifySave.Visible = false;
            modifClean.Visible = false;
            tabProviderGestor.SelectedTab = tabPageEditProvider;
            }
            catch (BusinessException be)
            {
                MessageBox.Show(this, be.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
            }

        }

        private void IsRowSelected() {

            if (dataGridViewProvider.SelectedCells.Count == 0)
            {
                throw new BusinessException("Porfavor seleccione un proveedor para modificarlo");
              

            }
        }

        private void ModifQuit_Click(object sender, EventArgs e)
        {
            foreach (Control c in tabPageEditProvider.Controls) {
                if (c is TextBox) {
                    c.Text = string.Empty;
                }
            }
            tabProviderGestor.SelectedTab = tabPageSearchProvider;
        }
        private void cleanSearch() {
            panelSearchProvider.Visible = false;
            txtSearch.Text = string.Empty;
            checkBoxActive.Checked = false;
        
        }

        private void ModifySave_Click(object sender, EventArgs e)
        {
            try
            {
                if (presenter.Update())
                {
                    MessageBox.Show(this, "El proveedor se actualizo correctamente", "Felicidades", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.RightAlign);
                    tabProviderGestor.SelectedTab = tabPageMainProvider;
                }
            }
            catch (BusinessException be) {
                MessageBox.Show(this, be.Message, "Error de datos", MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
                                   MessageBoxOptions.RightAlign);
            }
            catch (Exception ex) {
                MessageBox.Show(this, ex.Message, "Error No especifico", MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
                                   MessageBoxOptions.RightAlign);
            
}
        }

        private void modifClean_Click(object sender, EventArgs e)
        {
            foreach (Control c in tabPageEditProvider.Controls) {
                if ((c is TextBox) && (!(c.Enabled == false))) {
                    c.Text = string.Empty;
                
                }
            
            }
        }

        private void FormProviderView_Load(object sender, EventArgs e)
        {
            if (flag.Provider == "BrowserProviderPurchase")
            {
                tabProviderGestor.SelectedTab = tabPageSearchProvider;
                btnShowProvider.Visible = false;
                btnDeleteProvider.Visible = false;
                btnModifyProvider.Visible = false;
                enablePanel.Visible = false;
                buttonAddToList.Visible = true;
            }
            else
                tabProviderGestor.SelectedTab = tabPageMainProvider;
        }

        private void buttonAddToList_Click(object sender, EventArgs e)
        {
            if (dataGridViewProvider.Rows.Count != 0 && dataGridViewProvider.SelectedCells.Count!=0)
            {
                variablePurchase.ProviderCode = dataGridViewProvider.CurrentRow.Cells[0].Value.ToString();
                variablePurchase.ProviderName = dataGridViewProvider.CurrentRow.Cells[1].Value.ToString();
                Close();
            }
            else
                MessageBox.Show(this, "No se selecciono ningun registro!", "Seleccionar Campo", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
        }
    }
}
